<?php
class PNSpaMenu extends PNObject {
  function PNSpaMenu($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'spaservice_spamenu';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';
    
    $this->_objJoin[]     = array ( 'join_table'  =>  'spaservice_spacategory',
                            'join_field'          =>  array('id','title','titleTranslate'),
                            'object_field_name'   =>  array('category_id','category_title','category_titleTranslate'),
                            'compare_field_table' =>  'spaCategory_id',
                            'compare_field_join'  =>  'id');

    $this->_init($init, $where);
  }
  
  function getDataFromInputJSON($key = null, $default = null, $source = 'REQUEST')
  {
      if (!$key) {
          $key = $this->_objPath;
      }

      $obj = FormUtil::getPassedValue($key, $default, $source);
      $obj = json_decode($obj);
      if ($obj) {
          $this->_objData = $obj;
          $this->getDataFromInputPostProcess();
          return $this->_objData;
      }

      return $default;
  }
  
  function insertPostProcess()
  {
    $spaMenuTime = FormUtil::getPassedValue('spaMenuTime',FALSE);
    
    $spaMenuTimeTable = 'spaservice_spamenutime';

    foreach($spaMenuTime AS $value){
      $object[]  = array(
                'price' => $value['price'],
                'timeMins' => $value['timeMins'],
                'spaMenu_id' => $this->_objData['id']
          );
    }
    
    //Do the insert
    DBUtil::insertObjectArray($object, $spaMenuTimeTable);
  }
  
  function updatePostProcess()
  {
    
    $spaMenuTime = FormUtil::getPassedValue('spaMenuTime',FALSE);
    $spaMenuTimeTable = 'spaservice_spamenutime';
    
    $objID = $this->_objData[id];
    $result = NULL;
    if(isset($objID)){
      $pntables = pnDBGetTables();
      $tableSpaMenuTime  = $pntables['spaservice_spamenutime'];
      $columnSpaMenuTime = $pntables['spaservice_spamenutime_column'];
    
      $sql = "SELECT
                $tableSpaMenuTime.$columnSpaMenuTime[id],
                $tableSpaMenuTime.$columnSpaMenuTime[price],
                $tableSpaMenuTime.$columnSpaMenuTime[timeMins]
              FROM
                $tableSpaMenuTime
              WHERE 
                $tableSpaMenuTime.$columnSpaMenuTime[spaMenu_id] = $objID
              ORDER BY 
                $tableSpaMenuTime.$columnSpaMenuTime[id] ASC ";
      
      $column = array("id", "price","timeMins");
      $sqlResult = DBUtil::executeSQL($sql);
      $oldSpaMenuTime = DBUtil::marshallObjects ($sqlResult, $column);
    }
    
    
    
    $oldSpaMenuTimeItem = count($oldSpaMenuTime);
    foreach($spaMenuTime AS $value){
      if(isset($value['id'])){
        $updateObject[]  = array(
                'id' => $value['id'],
                'price' => $value['price'],
                'timeMins' => $value['timeMins'],
                'spaMenu_id' => $this->_objData['id']
        );
        for($i=0;$i<=$oldSpaMenuTimeItem;$i++){
          echo "oldSpaMenuTime[$i] : ".$oldSpaMenuTime[$i][id]." == ".$value[id]." : value";
          if($oldSpaMenuTime[$i]['id'] == $value['id']){
              unset($oldSpaMenuTime[$i]);
              echo " Deleted!";
          }
          echo " <HR>";
        }
        
        
      }
      if(!isset($value['id'])){
        $insertObject[]  = array(
                'price' => $value['price'],
                'timeMins' => $value['timeMins'],
                'spaMenu_id' => $this->_objData['id']
        );
      }
      
    }
    
    if(count($updateObject)>0){
       DBUtil::updateObjectArray($updateObject, $spaMenuTimeTable);
    }
    if(count($insertObject)>0){
       DBUtil::insertObjectArray($insertObject, $spaMenuTimeTable);
    }
    if(count($oldSpaMenuTime)>0){
      foreach($oldSpaMenuTime AS $value){
        $object[id] = $value[id];
        DBUtil::deleteObject($object,$spaMenuTimeTable);
      }
    }
   
  }
  
  function selectExtendResult(){      
      $objID = $this->_objData[id];
      $result = NULL;
      if(isset($objID)){

        $pntables = pnDBGetTables();
        $tableSpaMenuTime  = $pntables['spaservice_spamenutime'];
        $columnSpaMenuTime = $pntables['spaservice_spamenutime_column'];
      
        $sql = "SELECT
                  $tableSpaMenuTime.$columnSpaMenuTime[id],
                  $tableSpaMenuTime.$columnSpaMenuTime[price],
                  $tableSpaMenuTime.$columnSpaMenuTime[timeMins]
                FROM
                  $tableSpaMenuTime
                WHERE 
                  $tableSpaMenuTime.$columnSpaMenuTime[spaMenu_id] = $objID
                ORDER BY 
                  $tableSpaMenuTime.$columnSpaMenuTime[id] ASC ";
        
        $column = array("id", "price","timeMins");
        $sqlResult = DBUtil::executeSQL($sql);
        $result['spaMenuTime'] = DBUtil::marshallObjects ($sqlResult, $column);
      }
    return $result;
    
  }
}